import copy

from django.utils.safestring import mark_safe
from django.http.request import QueryDict

'''自定义分页组件'''
'''
    data_dict = {

    }
    queryset = models.UserInfo.objects.filter(**data_dict)
    page_object = Pagenation(request,queryset)

    page_queryset = page_object.page_queryset
    page_string = page_object.html()
    data = {
        "queryset":page_queryset,
        "page_string":page_string,
    }
'''

'''视图'''
"""
    <div class="clearfix">
        <ul class="pagination" style="float: left">
            {{ page_string }}
        </ul>
    </div>
"""
"""
    分页初始化
    :param current_page: 当前页码
    :param per_page: 每页显示数据条数
    :param all_count: 数据库中总条数
    :param base_url: 基础URL
    :param page_param: QueryDict对象，内部含所有当前URL的原条件
    :param plus: 页面上最多显示的页码数量
"""
class Pagenation(object):
    def __init__(self,request,queryset,page_size=10,plus=5,page_param="page"):
        page = request.GET.get(page_param,"1")
        if page.isdecimal():
            page = int(page)
        else:
            page = 1
        self.page_param = page_param
        self.page = page
        self.page_size = page_size

        self.start = (page - 1) * page_size
        self.end = page * page_size

        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True

        self.query_dict = query_dict
        # print(self.query_dict,"******************")
        '''分完页的数据'''
        self.page_queryset = queryset[self.start:self.end]

        '''计算总条数'''
        self.total_count = queryset.count()

        page_total, v = divmod(self.total_count, self.page_size)
        if v:
            page_total += 1
        self.page_total = page_total
        self.plus = plus
        if page_total <= 2 * plus + 1:
            self.start_page = 1
            self.end_page = page_total
        else:
            if page <= plus:
                self.start_page = 1
                self.end_page = 2 * plus + 1
            else:
                if page >= page_total - plus:
                    self.start_page = page_total - 2 * plus
                    self.end_page = page_total
                else:
                    self.start_page = page - plus
                    self.end_page = page + plus


    def html(self):
        page_list = []
        self.query_dict.setlist(self.page_param, [1])
        page_list.append('<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), "首页"))
        # page_list.append('<li><a href="javascript:void(0);" onclick="home_page();" tt="{}">{}</a>'.format(self.query_dict.urlencode(), "首页"))
        if self.page == 1:
            self.query_dict.setlist(self.page_param, [1])
            page_list.append('<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), "上一页"))
        else:
            self.query_dict.setlist(self.page_param, [self.page - 1])
            page_list.append('<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), "上一页"))
        for i in range(self.start_page, self.end_page + 1):
            if i == self.page:
                self.query_dict.setlist(self.page_param, [i])
                ele = '<li class="active"><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), i)
            else:
                self.query_dict.setlist(self.page_param, [i])
                ele = '<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), i)
            page_list.append(ele)

        if self.page == self.page_total:
            self.query_dict.setlist(self.page_param, [self.page_total])
            page_list.append('<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), "下一页"))
        else:
            self.query_dict.setlist(self.page_param, [self.page + 1])
            page_list.append('<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), "下一页"))
        self.query_dict.setlist(self.page_param, [self.page_total])
        page_list.append('<li><a href="?{}">{}</a>'.format(self.query_dict.urlencode(), "尾页"))

        go_string = """
                <li>
                        <form method="get" style="float: left;margin-left: -1px">
                            <input type="text" name="{}"
                                   style="position: relative;float: left;display:inline-block;width: 60px;border-radius: 0;"
                                   class="form-control" placeholder="页码">
                            <button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>
                        </form>
                    </li>
                """
        page_list.append(go_string.format(self.page_param))
        page_list_str = "".join(page_list)
        page_string = mark_safe(page_list_str)
        if not self.total_count:
            page_string = ""
        return page_string